package org.catdroid.englishformusicians;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class AdapterAllScreen extends ArrayAdapter {
	Activity a = null;
	List<Sentence> sentences = null;
	Resources resources = null;

	// private TextToSpeech tts;

	public AdapterAllScreen(Activity a, List<Sentence> sentences) {
		super(a, R.layout.row_all, sentences);
		this.a = a;
		this.sentences = sentences;
		// tts = new TextToSpeech(a, this);
		this.resources = a.getResources();
	}

	public View getView(final int pos, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
			LayoutInflater mInflater = LayoutInflater.from(a);
			convertView = mInflater.inflate(R.layout.row_test, null);
			// Creates a ViewHolder and store references to the two children
			// views we want to bind data to.
			holder = new ViewHolder();
			holder.linia1 = (TextView) convertView.findViewById(R.id.linia1);
			holder.linia2 = (TextView) convertView.findViewById(R.id.linia2);

			holder.play = (ImageView) convertView.findViewById(R.id.play);
			holder.mic = (ImageView) convertView.findViewById(R.id.mic);
			holder.like = (ImageView) convertView.findViewById(R.id.like);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Sentence sentence = sentences.get(pos);

		holder.linia1.setText(sentence.textoVO);
		holder.linia2.setText(sentence.textoTraducido);

		if (TestRepository.isChecked(a, MainActivity.cleanSentenceString(sentence.textoVO))) {
			holder.like.setVisibility(View.VISIBLE);
		} else {
			holder.like.setVisibility(View.INVISIBLE);
		}

		holder.mic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity.lastSentence = sentence.textoVO;
				GoogleVoiceUtil.launchGoogleVoice(a, sentence.textoVO);
			}
		});

		holder.play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int id = resources.getIdentifier("p" + (pos + 1), "raw", "org.catdroid.englishformusicians");
				final MediaPlayer mPlayer = MediaPlayer.create(a, id);
				mPlayer.start();
			}
		});

//		final MediaPlayer mPlayer = MediaPlayer.create(a, R.raw.p1);
//		convertView.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				try {
//					mPlayer.start();
//				} catch (IllegalStateException e) {
//					e.printStackTrace();
//				}
//			}
//		});

		return convertView;
	}

	private class ViewHolder {
		TextView linia1;
		TextView linia2;

		ImageView play;
		ImageView mic;
		ImageView like;
	}
}